//
//  CountDownModel.m
//  DeliciousFoodSquare
//
//  Created by ihope99.com on 16/4/25.
//  Copyright © 2016年 FFB. All rights reserved.
//

#import "CountDownModel.h"

static CountDownModel *mDownModel = nil;

@implementation CountDownModel{
    NSTimer *mTimer;
    int mTimeInterVal;
    NSTimeInterval myTimeInterval;
}


+ (CountDownModel *)Share {
    if (!mDownModel) {
        mDownModel = [[CountDownModel alloc] init];
    }
    return mDownModel;
}

@synthesize mEndTimeInterval;

-(instancetype)init{
    self = [super init];
    if (self) {
        mTimeInterVal = 0;
        _mStartTimeInterval = 0;
        _index = 0.2;
        self.is_runk = NO;
        

        [self showTimer];

        
    }
    return self;
}
-(void)setIndex:(float)index{
    _index = index;
    [self showTimer];
}
-(void)setIs_runk:(BOOL)is_runk{
    _is_runk = is_runk;
}
-(void)showTimer{
    mTimer = [NSTimer scheduledTimerWithTimeInterval:_index target:self selector:@selector(mTimerAction:) userInfo:nil repeats:YES];
    [mTimer setFireDate:[NSDate distantFuture]];
}

-(void)PasueTimer{
    [mTimer setFireDate:[NSDate distantFuture]];
}
-(void)StartTimer{
    [mTimer setFireDate:[NSDate distantPast]];
}

-(void)StartWithEndTimeInterval:(NSTimeInterval)endTimeInterval BOLL:(BOOL)is_ru Block:(void(^)(DownModel *model))block{
    
    self.resultBlock = block;
    
    self.is_runk = is_ru;
    
    mEndTimeInterval = endTimeInterval;
    
    if (_is_runk == YES) {
        _mStartTimeInterval = [CountDownModel getNewDate];
    }else{
        _mStartTimeInterval = 0;
    }
    
    mTimeInterVal = mEndTimeInterval - _mStartTimeInterval;
    
    if (mTimeInterVal <= 0) {
        [self CancelTimer];
    }
    else{
        [mTimer setFireDate:[NSDate distantPast]];
    }
    
}
-(void)mTimerAction:(NSTimer *)timer{
    
    if (mTimeInterVal <=0) {
        [self CancelTimer];
        return;
    }

    if (_is_runk != 0) {
         _mStartTimeInterval = [CountDownModel getNewDate];
    }
    
    _mStartTimeInterval += _index;
    mTimeInterVal = mEndTimeInterval - _mStartTimeInterval;
    
    
    int myTimeInterVal = (int)mTimeInterVal;
    
    DownModel *model = [[DownModel alloc]init];
    
    model.seconds = myTimeInterVal % 60;
    model.minutes = (myTimeInterVal / 60) % 60;
    model.hours = (myTimeInterVal/3600)%24;
    model.days = mTimeInterVal/(3600 * 24);
    NSLog(@"%02d天%02d时%02d分%02d秒",model.days,model.hours,model.minutes,model.seconds);
    
    if (self.resultBlock) {
        self.resultBlock(model);
    }
    
}

- (void)CancelTimer {
    if (mTimer) {
        [mTimer invalidate];
        mTimer = nil;
    }
}

+(NSTimeInterval)getNewDate
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval a=[currentDate timeIntervalSince1970];
    return a;
}
+(NSTimeInterval)showDataWith:(NSString *)str{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSDate *date = [formatter dateFromString:str];
    
    NSTimeInterval mTimeInterVal = (long)[date timeIntervalSince1970];
    return mTimeInterVal;
}
-(void)Cancel{
    self.resultBlock = nil;
    [self CancelTimer];
}
-(void)dealloc{
    self.resultBlock = nil;
    [self CancelTimer];
}


@end

@implementation DownModel

-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

@end
